﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using MacroDictionary = System.Collections.Generic.Dictionary<System.Guid, MetaNote.Model.Macro>;

namespace MetaNote.Model
{
    [Serializable()]
    public class SimpleMenuItem
    {
        public List<SimpleMenuItem> Children { get; set; }

        public Guid? macroKey { get; set; }
        public string Comment { get; set; }
        public MenuItemType MenuType { get; set; }
        public string ImageFile { get; set; }

        public SimpleMenuItem()
        {

        }

        public SimpleMenuItem(ToolStrip toolStrip)
        {
            this.Comment = toolStrip.Text;
            //toolStrip.Name + ": " + 
            //Todo: the tag may not contain a guid
            if (toolStrip.Tag != null)
            {
                this.macroKey = (Guid)toolStrip.Tag;
            }
            this.Children = new List<SimpleMenuItem>();
            foreach (ToolStripItem i in toolStrip.Items)
            {
                PopulateChildren(this.Children, i);
            }

        }


        public SimpleMenuItem(ToolStripItem toolStripItem)
        {
            this.Comment = toolStripItem.Text;// +": " + toolStripItem.Name;
            this.MenuType = toolStripItem.ToMenuType();
            if (toolStripItem.Tag != null)
            {
                this.macroKey = (Guid)toolStripItem.Tag;
            }
            this.Children = new List<SimpleMenuItem>();
            if (toolStripItem.Image != null)
            {
                Debug.WriteLine(toolStripItem.Image.ToString());

                var imageFileName = Path.Combine(Common.Settings.UserFilePath, toolStripItem.Image.GetHashCode().ToString() + ".png"); //m_Settings.UserFilePath
                this.ImageFile = imageFileName;
                if (!File.Exists(imageFileName))
                {
                    (toolStripItem.Image as System.Drawing.Bitmap).Save(imageFileName);
                }
            }
        }

        private void PopulateChildren(List<SimpleMenuItem> children, ToolStripItem toolStripItem)
        {

            var item = new SimpleMenuItem(toolStripItem);
            children.Add(item);

            if (toolStripItem is ToolStripDropDownItem)
            {
                foreach (ToolStripItem childItem in (toolStripItem as ToolStripDropDownItem).DropDownItems)
                {
                    PopulateChildren(item.Children, childItem);
                }
            }


        }

        internal ToolStripItem ToToolStripItem(MacroDictionary macros, EventHandler MacroButton_Click)
        {
            Macro macro;

            if (!this.macroKey.HasValue) {
                macro = InternalMacro();
            }
            else if (!macros.Keys.Contains(this.macroKey.Value))
            {
                //No such macro!
                //TODO: maybe link most items to internal commands.
                macro = MissingMacro();

            }
            else
            {
                macro = macros[this.macroKey.Value];
            }


            //TODO: create the *relevant* type (not always toolstripdropdown button)
            //var item = new System.Windows.Forms.ToolStripDropDownButton(macro.Title, macro.Image, MacroButton_Click, macro.Key.ToString() );
            ToolStripItem  item;
            switch (this.MenuType)
            {
                case (MenuItemType.ToolStripMenu):
                    item = new System.Windows.Forms.ToolStripMenuItem(macro.Title, macro.Image, MacroButton_Click, macro.Key.ToString());
                    
                    break;
                case (MenuItemType.Separator):
                    item = new System.Windows.Forms.ToolStripSeparator();
                    break;
                case MenuItemType.ToolStripButton:
                    item = new System.Windows.Forms.ToolStripButton(macro.Title, macro.Image, MacroButton_Click, macro.Key.ToString());
                    break;
                case MenuItemType.ToolStripSplitButton:
                    item = new System.Windows.Forms.ToolStripSplitButton(macro.Title, macro.Image, MacroButton_Click, macro.Key.ToString());
                    break;
                default:
                    throw new ArgumentException("Unknown tool strip item type: " + this.MenuType.ToString());           
            }
            
            item.Tag = macro.Key;


            if (!string.IsNullOrEmpty(this.ImageFile) && File.Exists(this.ImageFile))
            {
                item.Image = new Bitmap(this.ImageFile);
            }

            return item;
        }

        private Macro MissingMacro()
        {
            var m = new Macro();
            m.Title = "Missing";
            return m;
        }

        private Macro InternalMacro()
        {
            var m = new Macro();
            m.Title = this.Comment;
            return m;
        }


    }

    public enum MenuItemType
    {
        ToolStripMenu,
        ToolStripButton,
        Separator,
        ToolStripSplitButton,
    }
}
